﻿<%@ Page Title="部门管理" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet">
<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="../assets/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="../assets/img/validate/validate.css" rel="stylesheet">
<style type="text/css">
@media (min-width: 768px) {
	.modal-dialog {
		width: 768px;
		margin: 30px auto;
	}
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
	<div class="system-box">
		<div class="left-side  ibox-content">
			<div id="using_json"></div>
		</div>
		<div class="right-side">
			<div class="btns-container">
				<%--<button type="button" id="refreshBtn" class="btn btn-success">
					<i class="fa fa-refresh"></i>&nbsp;&nbsp;刷新
				</button>
				<button type="button" id="addBtn" class="btn btn-primary" data-toggle="modal" data-target="#addWindow">
					<i class="fa fa-plus"></i>&nbsp;&nbsp;新增
				</button>--%>

				<div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
					<form id="checkModuleForm" method="get" action="#">
					<div class="modal-dialog">
						<div class="modal-content animated bounceInRight">
							<div class="modal-header">
								<span class="editTitle">新增部门</span>
								<button type="button" class="close" data-dismiss="modal">
									<span aria-hidden="true">&times;</span>
									<span class="sr-only">关闭</span>
                                </button>
							</div>
							<div id="message" style="display: none"></div>
							<div class="modal-body">
								<table class="module-info" cellpadding="0" cellspacing="0">
									<thead></thead>
									<tbody>
										<tr>
                                            <td>
												<div class="form-group">
                                					<label class="control-label">部门名称&nbsp;&nbsp;</label>
                                					<input id="deptContent" name="deptContent" placeholder="请输入部门名称" 
                                						datacol="yes" err="部门名称" checkexpession="NotNull"
                                						maxlength="20" type="text" class="form-control input-w260 required"/>
                            					</div>
											</td>
											<td>
												<div class="form-group">
                                					<label class="control-label">部门全称&nbsp;&nbsp;</label>
                                                    <input id="deptName" name="deptName" maxlength="50" type="text" 
                                                    	datacol="yes" err="部门全称" checkexpession="NotNull" 
                                                    	class="form-control valid input-w288 required" readonly="readonly" aria-invalid="false">
                            					</div>
											</td>
										</tr>
										<tr>
											<td>
												<div class="form-group">
                                					<label class="control-label">部门类型&nbsp;&nbsp;</label>
                                					<select class="form-control m-b input-w260" id="kind" name="kind">
                                        				<option value="1">类型1</option>
                                        				<option value="2">类型2</option>
                                    				</select>
                            					</div>
											</td>
											<td>
												<div class="form-group hide" style="overflow: inherit;">
                                					<label class="control-label" style="margin-left: -26px;">成本中心编码&nbsp;&nbsp;</label>
                                					<input id="costCode" name="costCode" placeholder="在此输入编码" 
                                						maxlength="15" type="text" class="form-control input-w288" />
                            					</div>
                            					
												<div class="form-group hide">
                                					<label class="control-label">部门编码&nbsp;&nbsp;</label>
                                					<input id="deptCode" name="deptCode" maxlength="10" type="text" class="form-control" />
                                                    <span style="color: #f00;">*</span>
                            					</div>
                                                <div class="form-group">
                                					<label class="control-label">排序序号&nbsp;&nbsp;</label>
                                					<input id="sort" name="sort" maxlength="10" type="number" class="form-control input-w288" />
                            					</div>
											</td>
										</tr>
										
										<tr class="hide">
											<td>&nbsp;</td>
											<td>
												<div class="form-group">
                                					<label class="control-label">休息日套</label>
                                					<select class="form-control m-b" id="restsetid" name="restsetid">
                                						<option value="0">默认</option>
                                        				<option value="1">休息1</option>
                                        				<option value="2">休息2</option>
                                    				</select>
                            					</div>
											</td>
										</tr>
										
										<tr>
											<td>
												<div class="form-group">
                                					<label class="control-label">公司代码&nbsp;&nbsp;</label>
                                					<input id="companyID" name="companyID" placeholder="请输入公司代码" 
                                						maxlength="20" type="text" class="form-control input-w260 "/>
                            					</div>
											</td>
											<td>
												<div class="form-group hide">
                                					<label class="control-label">店铺编号&nbsp;&nbsp;</label>
                                					<input id="storeCode" name="storeCode" placeholder="在此输入店铺编号" 
                                						maxlength="20" type="text" class="form-control input-w288" />
                            					</div>
											</td>
										</tr>
										
										<tr class="hide">
											<td>
												<div class="form-group">
													<label class="control-label">店铺地址&nbsp;&nbsp;</label>
													<input id="storeAddr" name="storeAddr" placeholder="在此输入店铺地址" 
														maxlength="100" type="text" class="form-control input-w288" />
												</div>
											</td>
										</tr>
									</tbody>
								</table>
							</div>
							<div class="modal-footer">
								<button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
								<button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
							</div>
						</div>
					</div>
					</form>
				</div>
			</div>
			
			<div class="right-content-box">
				<div class="ibox-content">
					<table class="footable table table-stripped table-list">
						<thead></thead>
						<tbody id="pageList"></tbody>
					</table>
            	</div>
			</div>

		</div>
	</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
<script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
<script src="../assets/js/plugins/footable/footable.all.min.js"></script>
<script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
<script src="../assets/js/page/dept.js"></script>
<script src="../assets/js/page/rScroll.js"></script>
<script src="../assets/js/page/tableControl.js"></script>
<script src="../assets/img/validate/learunui-validator.js"></script>
<script type="application/javascript">
    $(document).ready(function () {
        //初始化多选框
        $(".i-checks").iCheck({
            checkboxClass: "icheckbox_square-green",
        });

        //用于记录当前选中节点id（部门id）
        var deptId = 1;
        //用于记录当前选中节点的代码
        var deptCode = '1';
        //用于记录父节点的代码
        var parentCode = '';
        //用于记录当前选中节点grade
        var deptGrade = 1;
        //用于给予当前选中节点父id
        var deptParent = 0;
        //用于标记是新增还是编辑
        var isCreate = true;
        //用于记录当前节点的名称
        var deptContent = '';
        //用于记录所属部门
        var belong = '';
        //模块ID
        var ModuleID = Y.getUrlParam("ModuleID");
        //初始树形列表
		var initTree = true;

        //加载导航列表
        var loadList = function (init) {
            //调用加载层
            layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
            initTree = true;
            Y.API2('Dept', 'GetList', { DeptCode: 1 }, function (data) {
                //关闭加载层
                layer.closeAll('dialog');
                var treeData = {};
                if (!Y.checkNull(data) && data.length > 0) {
                    //json处理
                    data.map(function (item, i) {
                        treeData.text = item.DeptContent;
                        treeData.data = {
                            Grade: item.Grade,
                            DeptID: item.DeptID,
                            DeptCode: item.DeptCode,
                            ParentID: item.ParentID,
                        };
                        if (item.Children.length > 0) {
                            treeData.state = { opened: true };
                            treeData.children = deptUtils.getChildNode(item.Children);
                        }
                    })

                    //初始化左侧菜单
                    $.jstree.destroy("#using_json");
                    $('#using_json').jstree({
                        core: {
                            data: [treeData]
                        }
                    }).bind('select_node.jstree', function (event, data) {
                    	initTree = false;
                        //绑定的点击事件
                        var node_data = data.node.data;
                        deptId = node_data.DeptID;
                        deptCode = node_data.DeptCode;
                        deptGrade = node_data.Grade;
                        deptParent = node_data.ParentID;
                        belong = data.node.text;

                        if (deptId == 1) {
                            $('#editBtn').attr('disabled', 'disabled');
                            $('#deleteBtn').attr('disabled', 'disabled');
                        } else {
                            $('#editBtn').removeAttr('disabled');
                            $('#deleteBtn').removeAttr('disabled');
                        }

                        //加载右侧列表
                        loadPageList();
                    });

                    //判断是否为初次调用
                    if (!Y.checkNull(init)) {
                        //获取初始化的相关属性
                        deptId = data[0]['DeptID'];
                        deptGrade = data[0]['Grade'];
                        deptParent = data[0]['ParentID'];
                        belong = data[0]['DeptContent'];
                        deptCode = data[0]['DeptCode'];
                        //加载右侧列表
                        loadPageList();

                        //禁用编辑和删除
                        $('#editBtn').attr('disabled', 'disabled');
                        $('#deleteBtn').attr('disabled', 'disabled');
                    }
                }
            });
        }
        loadList(true);

        //获取右侧内容列表
        var loadPageList = function () {
        	if($('.footable thead').children().size() == 0)
        		tableControl.initThead2('.footable',['编号','部门名称','部门全称','部门编码','排序','上级部门']);

            Y.API2('Dept', 'GetList', { DeptCode: deptCode }, function (data) {
                if (data.Success == false) {
                    swal(data.Message, '', 'error');
                } else {
                	var tBody = [];
                    if (!Y.checkNull(data) && data[0]['Children'].length > 0) {
                    	data[0]['Children'].map(function (item, i) {
                            item.belong = belong;
                        });
                    	
						data[0]['Children'].map(function(item,i){			
							var tempTr = {
								trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd' ),
								children: [
									{tdText: item.DeptID, c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
									{tdText: item.CompanyID, c:0 , r:0, tdHtml:'', tdId:'', tdClass:'hide'},
									{tdText: (item.DeptContent || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
									{tdText: (item.DeptName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
									{tdText: (item.DeptCode || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
									{tdText: (item.Sort), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
									{tdText: (item.belong), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
								]
							};
							tBody.push(tempTr);
						})
                    }
                    tableControl.initTbody('.footable',(tBody.length > 0 ? tBody : null),5,null);
                }
            });
        }
        var loadIndex = function () {
            Y.API2('Common', 'GetSort', { "tableName": "sys_dept", "sortName": "sort" }, function (data) {
                if (data.Success == false) {
                    //layer.msg(data.Message);
                } else {
                    $("#sort").val(data + 1);
                    $("#sort").attr("readonly", true);
                }
            });
        }
        //获取按钮
        commonUtil.loadBtnList(ModuleID,function(){
        	rScroll()
        });
        
        //刷新按钮
        $('.btns-container').on('click', '#refreshBtn', function () {
            loadList(true);
//	            loadPageList();
        });
        //新增按钮
        $('.btns-container').on('click', '#addBtn', function () {
        	if(initTree){
				swal('请先在目录树选择节点');
				return false;
        	}
        	$("#companyID").parent().addClass("hide")
        	$("#companyID").removeClass("required");
        	if (deptId == 1)
        	{
        	    $("#companyID").parent().removeClass("hide")
        	    $("#companyID").addClass("required");
        	}
            $('.editTitle').text('新增部门');
            isCreate = true;
            $(':input', '#checkModuleForm').not(':button, :submit, :reset').val('');
            $('#kind').val(1);
            $('#restsetid').val(1);
            $('#deptName').val(belong);//获取当前节点名称
            loadIndex();
        });
        //数据联动
        $("#deptContent").keyup(function () {
            var beginStr = $("#deptName").val().split("/")[0];
            var dept = $("#deptContent").val();
            $("#deptName").val(beginStr + (dept == "" ? "" : "/" + dept));
        });

        //编辑按钮ParentCode
        $('.btns-container').on('click', '#editBtn', function () {
            $("#companyID").parent().addClass("hide")
            $("#companyID").removeClass("required");
            if (deptParent == 1) {
                $("#companyID").parent().removeClass("hide")
                $("#companyID").addClass("required");
            }
            $('.editTitle').text('编辑部门');
            isCreate = false;
            layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
            Y.API2('Dept', 'GetID', {
                DeptID: deptId,
            }, function (data) {
                layer.closeAll('dialog');
                if (data.Success == false) {
                    swal(data.Message, '', 'error');
                } else {
                    $('#companyID').val(data.CompanyID);
                    $('#deptName').val(data.DeptName);
                    $('#deptContent').val(data.DeptContent);
                    $('#deptCode').val(data.DeptCode);
                    $('#kind').val(data.Kind);
                    $('#costCode').val(data.CostCode);
                    $('#restsetid').val(data.Restsetid);
                    $('#sort').val(data.Sort);
                    $('#storeCode').val(data.StoreCode);
                    $('#storeAddr').val(data.StoreAddr);
                    parentCode = data.ParentCode;
                }
            })
        });

        //点击删除
        commonUtil.initDeleteBtnEvent({
            callBack: function () {
                Y.API2('Dept', 'Remove', { DeptID: deptId }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal('删除失败', data.Message, 'error');
                    } else {
                        swal('删除成功', '该信息已被你永久删除', 'success');
                        $('#refreshBtn').click();
                    }
                });
            }
        });
        
        //检验提交信息
        $('#addModuleBtn').click(function () {
            var CompanyID = $('#companyID').val();
        	if (!CheckDataValid('#checkModuleForm')) {
                return false;
        	}
        	if (deptId == 1 && Y.checkNull(CompanyID)) {
        	    tipCss($('#companyID'), "公司代码不能为空！");
        	    return false;
        	}

        	//检验通过后执行新增或者编辑
            var deal = '';
            var parentid = 0;
            var successText = '';
            if (isCreate) {
                deal = 'Create';
                successText = '新增成功';
            } else {
                deal = 'Modify';
                successText = '编辑成功';
            }
            
            layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
            Y.API2('Dept', deal, {
                DeptID: deptId,
                CompanyID: CompanyID,
                DeptCode: $('#deptCode').val(),
                DeptContent: $('#deptContent').val(),
                Kind: $('#kind').val(),
                DeptName: $('#deptName').val(),
                CostCode: $('#costCode').val(),
                Restsetid: $('#restsetid').val(),
                StoreCode: $('#storeCode').val(),
                StoreAddr: $('#storeAddr').val(),
                ParentID: isCreate ? deptId : deptParent,
                ParentCode: isCreate ? deptCode : parentCode,
                Grade: isCreate ? (deptGrade + 1) : deptGrade,
                Sort: $('#sort').val()
            }, function (data) {
                layer.closeAll('dialog');
                if (data.Success == false) {
                    swal(data.Message, '', 'error');
                } else {
                    swal(successText, '', 'success');
                    //关闭新建  or 编辑框
                    $('#closeAddBox').click();
                    //刷新列表
                    $('#refreshBtn').click();
                }
            })
            
        })
    });
</script>
</asp:Content>